package com.onemile.app;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.onemile.utils.UtiDialogsFactory;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class OneMileActivity extends MapActivity
{
   MapView mapView;

   private int CONNECTION_TIMEOUT = 20000;
   private int SO_TIMEOUT = 20000;

   float lat;
   float lon;


   @Override
   public void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
 	  setContentView(R.layout.main);
	  Button btn1 = (Button) findViewById(R.id.button1);// ����
	  Button btn2 = (Button) findViewById(R.id.button2);// ��ͨ
	  Button btn = (Button) findViewById(R.id.button3);// ��λ
	  Button btnBaidu=(Button)findViewById(R.id.btnBaidu);
	  mapView = (MapView) findViewById(R.id.map1);
	  mapView.setSatellite(true);
	  btn1.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			mapView.setSatellite(true);
		 }
	  });

	  btn2.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO Auto-generated method stub
			mapView.setSatellite(false);
		 }
	  });

	  btn.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO Auto-generated method stub
			try
			{

			   UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(OneMileActivity.this,"loading");
			   loader l = new loader();
			   l.execute("");

			}
			catch (Exception e)
			{
			   e.printStackTrace();
			}

		 }
	  });
	  
	  btnBaidu.setOnClickListener(new OnClickListener()
	  {  
	     @Override
	     public void onClick(View v)
	     {
	  	   Intent intent=new Intent();
	  	   intent.setClass(OneMileActivity.this, UIbaidu.class);
	  	   startActivity(intent);
	     }
	  });

   }

   @Override
   protected boolean isRouteDisplayed()
   {
	  // TODO Auto-generated method stub
	  return false;
   }

   class loader extends AsyncTask<String, Integer, String>
   {

	  @Override
	  protected String doInBackground(String... params)
	  {
		 TelephonyManager tm = (TelephonyManager) OneMileActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
		 try
		 {
			GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
			int cid = gcl.getCid();
			int lac = gcl.getLac();
			int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3));
			int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5));
			String IMEI = tm.getDeviceId();

			// ��װJSON��ѯ�ַ�
			JSONObject holder = new JSONObject();
			holder.put("version", "1.1.0");
			holder.put("host", "maps.google.com");
			// holder.put("address_language", "zh_CN");
			holder.put("request_address", true);

			JSONArray array = new JSONArray();
			JSONObject data = new JSONObject();
			data.put("cell_id", cid); // 25070
			data.put("location_area_code", lac);// 4474
			data.put("mobile_country_code", mcc);// 460
			data.put("mobile_network_code", mnc);// 0
			array.put(data);
			holder.put("cell_towers", array);

			// �������ӣ��������󲢽��ܻ�Ӧ
			DefaultHttpClient client = new DefaultHttpClient();

			client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
			client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);

			HttpPost post = new HttpPost("http://www.google.com/loc/json");

			StringEntity se = new StringEntity(holder.toString());

			post.setEntity(se);
			HttpResponse resp = client.execute(post);

			HttpEntity entity = resp.getEntity();

			BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
			StringBuffer sb = new StringBuffer();
			String result = br.readLine();

			while (result != null)
			{
			   sb.append(result);
			   result = br.readLine();
			}
			JSONObject jsonObject = new JSONObject(sb.toString());

			JSONObject jsonObject1 = new JSONObject(jsonObject.getString("location"));

			lat = Float.valueOf(jsonObject1.getString("latitude"));
			lon = Float.valueOf(jsonObject1.getString("longitude"));
		 }
		 catch (Exception e)
		 {
			// TODO: handle exception
			e.printStackTrace();
		 }
		 return null;
	  }

	  @Override
	  protected void onPostExecute(String result)
	  {
		 UtiDialogsFactory.getInstance().DismissLoadingDialog();

		 GeoPoint p = new GeoPoint((int) (lat * 1000000), (int) (lon * 1000000));
//		 GeoPoint p = new GeoPoint((int) (30 * 1000000), (int) (120 * 1000000));

		 // ȡ�õ�ͼView�Ŀ���
 		 MapController mc = mapView.getController();

		 mc.animateTo(p);

		 // ���ó�ʼ������
		 mc.setZoom(17);
	  }

   }

}